<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="账户号:">
        <el-input v-model="queryParams.accountNo" size="small" placeholder="请输入账户号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="银行名称:">
        <el-input v-model="queryParams.bankName" size="small" placeholder="请输入银行名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属用户编码:">
        <el-input v-model="queryParams.customerId" size="small" placeholder="请输入所属用户编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="当前金额:">
        <el-input v-model="queryParams.amount" size="small" placeholder="请输入当前金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名称:">
        <el-input v-model="queryParams.customerName" size="small" placeholder="请输入用户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accMyAccountWallet:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accMyAccountWallet:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accMyAccountWallet:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accMyAccountWallet:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户号" align="center" prop="accountNo" />
      <el-table-column label="银行名称" align="center" prop="bankName" />
      <el-table-column label="所属用户编码" align="center" prop="customerId" />
      <el-table-column label="用户类型" align="center" prop="customerType" />
      <el-table-column label="当前金额" align="center" prop="amount" />
      <el-table-column label="用户名称" align="center" prop="customerName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['accounting:accMyAccountWallet:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['accounting:accMyAccountWallet:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="accMyAccountWalletFormDialog.title" :visible.sync="accMyAccountWalletFormDialog.visible" :close-on-click-modal="false" :before-close="resetAccMyAccountWalletFormDialog" width="500px" append-to-body>
      <el-form ref="accMyAccountWalletForm" :model="accMyAccountWalletFormData" :rules="accMyAccountWalletFormRules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="accMyAccountWalletFormData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="账户号" prop="accountNo">
          <el-input v-model="accMyAccountWalletFormData.accountNo" placeholder="请输入账户号" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="accMyAccountWalletFormData.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="所属用户编码" prop="customerId">
          <el-input v-model="accMyAccountWalletFormData.customerId" placeholder="请输入所属用户编码" />
        </el-form-item>
        <el-form-item label="当前金额" prop="amount">
          <el-input v-model="accMyAccountWalletFormData.amount" placeholder="请输入当前金额" />
        </el-form-item>
        <el-form-item label="用户名称" prop="customerName">
          <el-input v-model="accMyAccountWalletFormData.customerName" placeholder="请输入用户名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmAccMyAccountWalletFormDialog">确 定</el-button>
        <el-button @click="resetAccMyAccountWalletFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AccMyAccountWalletApi from '@/api/accounting/AccMyAccountWalletApi'

const defaultAccMyAccountWalletFormData = {
  accountNo: null,

  bankName: null,

  customerId: null,

  customerType: null,

  amount: null,

  customerName: null,

}
export default {
  name: 'AccMyAccountWalletList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        accountNo: null,
        bankName: null,
        customerId: null,
        customerType: null,
        amount: null,
        customerName: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      accMyAccountWalletFormDialog: {
        title: '',
        visible: false
      },
      accMyAccountWalletFormData: Object.assign({}, defaultAccMyAccountWalletFormData),
      accMyAccountWalletFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openAccMyAccountWalletFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return AccMyAccountWalletApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openAccMyAccountWalletFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      AccMyAccountWalletApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return AccMyAccountWalletApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openAccMyAccountWalletFormDialog(id) {
      this.accMyAccountWalletFormDialog.visible = true
      if (id) {
        this.accMyAccountWalletFormDialog.title = '我的钱包-修改'
        AccMyAccountWalletApi.get(id).then(response => {
          this.accMyAccountWalletFormData = {
            ...this.accMyAccountWalletFormData,
            ...response.data
          }
        })
      } else {
        this.accMyAccountWalletFormDialog.title = '我的钱包-添加'
      }
    },
    confirmAccMyAccountWalletFormDialog() {
      this.$refs['accMyAccountWalletForm'].validate(valid => {
        if (valid) {
          if (this.accMyAccountWalletFormData.id) {
            AccMyAccountWalletApi.edit(this.accMyAccountWalletFormData).then(response => {
              this.$message.success('修改成功')
              this.resetAccMyAccountWalletFormDialog()
              this.handleQuery()
            })
          } else {
            AccMyAccountWalletApi.add(this.accMyAccountWalletFormData).then(response => {
              this.$message.success('添加成功')
              this.resetAccMyAccountWalletFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetAccMyAccountWalletFormDialog() {
      this.accMyAccountWalletFormDialog = {
        title: '',
        visible: false
      }
      this.accMyAccountWalletFormData = Object.assign({}, defaultAccMyAccountWalletFormData)
      this.resetForm('accMyAccountWalletForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
